import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/baseblocs/bloc_provider.dart';
import 'package:shopping_group/http/apis/apis.dart';
import 'package:shopping_group/http/http_request_all.dart';
import 'package:shopping_group/modle/person/entity/user_friends_entity.dart';
import 'package:shopping_group/utils/network_utils.dart';
import 'package:shopping_group/utils/route_util.dart';

class MyTuanYouBloc implements BlocBase {
  StreamController<Object> _initController = StreamController<Object>();
  StreamSink get initDate => _initController.sink;

  StreamController<Object> _allController = StreamController<Object>();
  StreamSink get _addAllDate => _allController.sink;
  Stream<Object> get sendAllData => _allController.stream;

  MyTuanYouBloc() {
    _initController.stream.listen(allData);
  }

  void allData(data) {
    _allTask(data);
  }

  void _allTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext).requestFocus(FocusNode());
    Map<String, dynamic> data = {};
    data['val'] = BaseConstant.userId;
    //BotToast.showLoading();
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.TUANYOU_LIST_URL, data: data, headers: {}, success: (result) {
          BotToast.closeAllLoading();
          if (null != result) {
            UserFriendsEntity entity = UserFriendsEntity.fromJson(result);
            _addAllDate.add(entity);
            // eventBus.fire(UpTuanyouListEvent(entity.lstSameUp));
            // eventBus.fire(DownTuanyouListEvent(entity.lstChild));
          }
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  @override
  void dispose() {
    _initController?.close();
    _allController?.close();
  }
}
